package com.vornitologist.ui;

import org.activiti.engine.ProcessEngines;
import org.activiti.engine.form.TaskFormData;

import com.guruyu.hawaii.core.data.TaskData;
import com.guruyu.hawaii.interfaces.HawaiiFactory;
import com.guruyu.hawaii.ui.formProperty.Render.FormPropertiesForm;
import com.vaadin.addon.touchkit.ui.NavigationView;
import com.vaadin.addon.touchkit.ui.VerticalComponentGroup;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;

public class CompleteTaskView extends NavigationView{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4543854020960104151L;
	private Layout layout = new VerticalComponentGroup();
	private TaskData process;
	private CssLayout content;
	private TaskFormData formData;
	private FormPropertiesForm formTask;

	
	@SuppressWarnings("deprecation")
	
	private void buildView() {
		
		setCaption("Tarea " +process.getNombre());

		content = new CssLayout();
		content.setWidth("100%");

		VerticalComponentGroup componentGroup = new VerticalComponentGroup();
		//componentGroup.setCaption(process.getName());

		Label about = new Label(
				"<div style='color:#333;'><p>Descripcion del procesos "+process.getNombre()+" "+process.getDescripcion()
						,
				Label.CONTENT_XHTML);

		componentGroup.addComponent(about);

		content.addComponent(componentGroup);

		formData = ProcessEngines.getDefaultProcessEngine().getFormService().getTaskFormData(process.getId());
    	
		formTask = new FormPropertiesForm(null);
		formTask.setFormProperties(formData.getFormProperties());
		
		content.addComponent(formTask);
		
		Button continuar = new Button("Continuar");
		continuar.addListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				
				try{
					HawaiiFactory.getInstance().getIEngineController().submitTaskFormData(process.getId(), formTask.getFormPropertiesComponent()
							.getFormPropertyValues());
					getNavigationManager().navigateTo(new TaskManager());
				}catch(Exception e){
					getWindow().showNotification(e.getLocalizedMessage());
				}
				
				
			}
		});
		content.addComponent(continuar);
		setContent(content);
		
	}
	public CompleteTaskView(Object item){
				
		process = (TaskData) item; 
		super.attach();
		buildView();
		
	}

	protected void showProcessStart() {
    	
    	
	}
	
}
